package com.cms.action;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.action.DefaultAction;
import com.cms.model.AppInfo;
import com.cms.model.AppVersionInfo;
import com.cms.model.Channel;
import com.cms.model.ImportUserChannelData;
import com.cms.model.ImportUserData;
import com.cms.service.AppInfoService;
import com.cms.service.AppInfoServiceImpl;
import com.cms.service.AppVersionInfoServiceImpl;
import com.cms.service.ChannelService;
import com.cms.service.ChannelServiceImpl;
import com.cms.service.ImportUserDataService;
import com.cms.service.ImportUserDataServiceImpl;
import com.util.Util;

public class ImportUserQueryAction extends DefaultAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String startdate;
	String enddate; 
	
	String appid;
	String appversionnumber;
	int channelid;
	String operator;
	String dimension;
	public ImportUserQueryAction()
	{}
	List<AppInfo> applist;
	List<AppVersionInfo> appversionlist;
	List<Channel> channellist;
	ChannelService channelservice = new ChannelServiceImpl();
	AppInfoService appservice = new AppInfoServiceImpl();
	List<ImportUserData> list;
	List<ImportUserChannelData> channelulist;
	ImportUserDataService service = new ImportUserDataServiceImpl();
	
	String type;
	public String query()
	{
		applist = appservice.getAppInfos();
		appversionlist = new AppVersionInfoServiceImpl().getAppVersionInfos();
		channellist = channelservice.getChannelsByType("cpa");
		if(startdate==null || "".equals(startdate)||enddate==null || "".equals(enddate))
		{
			Calendar c = Calendar.getInstance();
			c.setTime(new Date());
			c.add(Calendar.DATE, -1);
			startdate =enddate= Util.dateToString(c.getTime(), "yyyy-MM-dd");
		}
		
		if("query".equals(type))
		{
			list = service.queryImportUserDataByContent(startdate, enddate, appid, appversionnumber,
					channelid, operator, dimension);
		}
		return SUCCESS;
	}
	public String channelquery()
	{
		applist = appservice.getAppInfos();
		appversionlist = new AppVersionInfoServiceImpl().getAppVersionInfos();
		channellist = channelservice.getChannelsByType("cpa");
		if(startdate==null || "".equals(startdate)||enddate==null || "".equals(enddate))
		{
			Calendar c = Calendar.getInstance();
			c.setTime(new Date());
			c.add(Calendar.DATE, -1);
			startdate =enddate= Util.dateToString(c.getTime(), "yyyy-MM-dd");
		}
		
		if("query".equals(type))
		{
			channelulist = service.queryImportUserChannelDataByContent(startdate, enddate, appid,appversionnumber, 
					channelid, operator, dimension);
		}
		return SUCCESS;
	}
	public String getchannelname(List<Channel> channellist,int channelid)
	{
		String channelname = "未知";
		for(int i =0;i<channellist.size();i++)
		{
			if(channellist.get(i).getId()==channelid)
			{
				return channellist.get(i).getName();
			}
		}
		return channelname;
	}
	public String changeappversion(){
		appversionlist = new AppVersionInfoServiceImpl().getAppVersionInfoByAppNumber(appid);
		return SUCCESS;
	}
	public String getStartdate() {
		return startdate;
	}

	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}

	public String getEnddate() {
		return enddate;
	}

	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public int getChannelid() {
		return channelid;
	}

	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}


	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getDimension() {
		return dimension;
	}

	public void setDimension(String dimension) {
		this.dimension = dimension;
	}

	public List<AppInfo> getApplist() {
		return applist;
	}

	public void setApplist(List<AppInfo> applist) {
		this.applist = applist;
	}

	public List<Channel> getChannellist() {
		return channellist;
	}

	public void setChannellist(List<Channel> channellist) {
		this.channellist = channellist;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<ImportUserData> getList() {
		return list;
	}

	public void setList(List<ImportUserData> list) {
		this.list = list;
	}
	public List<ImportUserChannelData> getChannelulist() {
		return channelulist;
	}
	public void setChannelulist(List<ImportUserChannelData> channelulist) {
		this.channelulist = channelulist;
	}
	public List<AppVersionInfo> getAppversionlist() {
		return appversionlist;
	}
	public void setAppversionlist(List<AppVersionInfo> appversionlist) {
		this.appversionlist = appversionlist;
	}
	public String getAppversionnumber() {
		return appversionnumber;
	}
	public void setAppversionnumber(String appversionnumber) {
		this.appversionnumber = appversionnumber;
	}
	
}
